package com.qnbc.mystery.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qnbc.mystery.mapper.CollectMapper;
import com.qnbc.mystery.pojo.domain.*;
import com.qnbc.mystery.service.ICartService;
import com.qnbc.mystery.service.IProductSwiperImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qnbc.mystery.mapper.BoxInfoMapper;
import com.qnbc.mystery.domain.BoxInfo;
import com.qnbc.mystery.service.IBoxInfoService;

/**
 * 奖盒管理Service业务层处理
 *
 * @author guzj
 * @date 2024-01-30
 */
@Service
public class BoxInfoServiceImpl extends ServiceImpl<BoxInfoMapper, BoxInfo> implements IBoxInfoService {
    @Autowired
    private BoxInfoMapper boxInfoMapper;

    @Autowired
    private ICartService cartService;

    @Autowired
    private IProductSwiperImageService productSwiperImageService;

    /**
     * 查询奖盒管理
     *
     * @param id 奖盒管理主键
     * @return 奖盒管理
     */
    @Override
    public BoxInfo selectBoxInfoById(Long id) {
        BoxInfo boxInfo = boxInfoMapper.selectBoxInfoById(id);
        List<ProductSwiperImage> list = productSwiperImageService.listBoxInfo(boxInfo);
        boxInfo.setProductSwiperImageList(list);
        return boxInfo;
    }

    /**
     * 查询奖盒管理列表
     *
     * @param boxInfo 奖盒管理
     * @return 奖盒管理
     */
    @Override
    public List<BoxInfo> selectBoxInfoList(BoxInfo boxInfo) {
        List<BoxInfo> boxInfos = boxInfoMapper.selectBoxInfoList(boxInfo);
        for (BoxInfo info : boxInfos) {
            //获取月销量
            LambdaQueryWrapper<Cart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            int count = (int) cartService.count(lambdaQueryWrapper.eq(Cart::getProductId, info.getId()));
            info.setMonthSales(count);

            LambdaQueryWrapper<ProductSwiperImage> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            List<ProductSwiperImage> list = productSwiperImageService.list(lambdaQueryWrapper1.eq(ProductSwiperImage::getProductid, info.getId()));
            info.setProductSwiperImageList(list);

        }
        return boxInfos;
    }

    /**
     * 新增奖盒管理
     *
     * @param boxInfo 奖盒管理
     * @return 结果
     */
    @Override
    public int insertBoxInfo(BoxInfo boxInfo) {
        return boxInfoMapper.insertBoxInfo(boxInfo);
    }

    /**
     * 修改奖盒管理
     *
     * @param boxInfo 奖盒管理
     * @return 结果
     */
    @Override
    public int updateBoxInfo(BoxInfo boxInfo) {
        return boxInfoMapper.updateBoxInfo(boxInfo);
    }

    /**
     * 批量删除奖盒管理
     *
     * @param ids 需要删除的奖盒管理主键
     * @return 结果
     */
    @Override
    public int deleteBoxInfoByIds(Long[] ids) {
        return boxInfoMapper.deleteBoxInfoByIds(ids);
    }

    /**
     * 删除奖盒管理信息
     *
     * @param id 奖盒管理主键
     * @return 结果
     */
    @Override
    public int deleteBoxInfoById(Long id) {
        return boxInfoMapper.deleteBoxInfoById(id);
    }
}
